/*
 * Configuration.java
 *
 * Created on September 10, 2006, 4:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package smfbackup.conf;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
import smfbackup.ErrorHandler;

/**
 *
 * @author Iulian
 */
public class Configuration {

    private Logger log = Logger.getLogger(Configuration.class);
    private final static String filename = "SMFBackup.conf";
    private static boolean configHasChanged;
    private static Vector<Forum> backupForumsDefined;
    private static Vector<Forum> forumsDefined;
    // some default properties
    private static String proxyHost = "";
    private static String proxyPort = "";
    private static boolean proxyEnabled = false;
    private static String backupDir = getCurrentPath();
    private static String loggingLevel = "Only Severe";
    //private static String loggingLevel = "Debug";
    // divider location
    private static String dividerLocation = "150";
    private static String lookAndFeel;
    private static String userAgent;

    private static String startupOption;

    static {
        forumsDefined = new Vector<Forum>();
        backupForumsDefined = new Vector<Forum>();
        configHasChanged = false;
    }

    /** Creates a new instance of Configuration */
    private Configuration() {
    }

    public static void backupSettings() {
        backupForumsDefined.clear();
        backupForumsDefined.addAll(forumsDefined);
    }

    public static void restoreSettings() {
        forumsDefined.clear();
        forumsDefined.addAll(backupForumsDefined);
    }

    public static Forum addNewForum() {
        Forum f = new Forum();
        f.setDefaults();
        // put it in the list of new forums
        forumsDefined.addElement(f);

        setConfigurationChanged();

        return f;
    }

    public static void removeForum(Forum f) {
        forumsDefined.remove(f);
        setConfigurationChanged();
    }

    private static String nvl(String arg) {
        if (arg != null) {
            return arg;
        } else {
            return "";
        }
    }

    public static void saveConfiguration() {

        Properties conf = new Properties();
        BufferedOutputStream bos = null;

        conf.setProperty("proxy.enabled", proxyEnabled + "");
        conf.setProperty("proxy.host", nvl(proxyHost));
        conf.setProperty("proxy.port", nvl(proxyPort));
        conf.setProperty("backup.directory", backupDir);
        conf.setProperty("logging.level", loggingLevel);
        conf.setProperty("divider.location", dividerLocation);
        conf.setProperty("startup.option", nvl(startupOption));
        conf.setProperty("user.agent", nvl(userAgent));

        // collect data
        for (int i = 0; i < forumsDefined.size(); i++) {
            conf.putAll(((Forum) forumsDefined.elementAt(i)).save("forum", i));
        }
        // update count
        conf.setProperty("forums.count", "" + forumsDefined.size());

        if (lookAndFeel != null) {
            conf.setProperty("look.and.feel", lookAndFeel);
        }

        try {
            bos = new BufferedOutputStream(new FileOutputStream(filename));
            conf.storeToXML(bos, "Generated automatically. Modify at your own risk :)");

            configHasChanged = false;
            Configuration.backupSettings();

        } catch (Throwable t) {
            ErrorHandler.showError("Unable to save configuration.", t);
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (Exception e) {
                    ErrorHandler.showError("Unable to close the configuration file.", e);
                }
            }
        }
    }

    public static void loadConfiguration() {
        Properties conf = new Properties();
        BufferedInputStream bis = null;

        try {

            if (!(new File(filename)).isFile()) {
                return;
            }

            bis = new BufferedInputStream(new FileInputStream(filename));
            conf.loadFromXML(bis);

            int forums = Integer.parseInt(conf.getProperty("forums.count"));

            forumsDefined = new Vector<Forum>();
            for (int i = 0; i < forums; i++) {

                Forum f = new Forum();
                f.load(conf, "forum", i);
                forumsDefined.addElement(f);
            }

            configHasChanged = false;

            lookAndFeel = conf.getProperty("look.and.feel");

            setProxyEnabled(Boolean.parseBoolean(conf.getProperty("proxy.enabled")));
            setProxyHost(conf.getProperty("proxy.host"));
            setProxyPort(conf.getProperty("proxy.port"));
            setBackupDir(conf.getProperty("backup.directory"));
            setLoggingLevel(conf.getProperty("logging.level"));
            setDividerLocation(conf.getProperty("divider.location"));
            setStartupOption(conf.getProperty("startup.option"));
            setUserAgent(conf.getProperty("user.agent"));

        } catch (Throwable t) {
            ErrorHandler.showError("Unable to load configuration.", t);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (Exception e) {
                    ErrorHandler.showError("Unable to close the configuration file.", e);
                }
            }
            ;
        }
    }

    private static String getCurrentPath() {
        return (new File("")).getAbsolutePath();
    }

    public static void setLookAndFeel(String newClass) {
        lookAndFeel = newClass;
    }

    public static String getLookAndFeel() {
        return lookAndFeel;
    }

    public static Vector<Forum> getForumsDefined() {
        return forumsDefined;
    }

    public static void setForumsDefined(Vector<Forum> aForumsDefined) {
        forumsDefined = aForumsDefined;
    }

    public static boolean isConfigurationChanged() {
        return configHasChanged;
    }

    public static void setConfigurationChanged() {
        configHasChanged = true;
    }

    public static String getDividerLocation() {
        return dividerLocation;
    }

    public static void setDividerLocation(String _newLoc) {
        dividerLocation = _newLoc;
    }

    public static String getProxyHost() {
        return proxyHost;
    }

    public static void setProxyHost(String aProxyHost) {
        proxyHost = aProxyHost;
    }

    public static String getProxyPort() {
        return proxyPort;
    }

    public static void setProxyPort(String aProxyPort) {
        proxyPort = aProxyPort;
    }

    public static boolean isProxyEnabled() {
        return proxyEnabled;
    }

    public static void setProxyEnabled(boolean aproxyEnabled) {
        proxyEnabled = aproxyEnabled;
    }

    public static String getBackupDir() {
        return backupDir;
    }

    public static void setBackupDir(String aBackupDir) {
        backupDir = aBackupDir;
    }

    public static String getLoggingLevel() {
        return loggingLevel;
    }

    public static void setLoggingLevel(String aLoggingLevel) {
        loggingLevel = aLoggingLevel;
    }

    public static String getStartupOption() {
        return startupOption;
    }

    public static void setStartupOption(String startupOption) {
        Configuration.startupOption = startupOption;
    }

    public static String getUserAgent() {
        return userAgent;
    }

    public static void setUserAgent(String userAgent) {
        if (userAgent == null || userAgent.trim().length() == 0)
            userAgent = "Jakarta Commons-HttpClient/3.1";
        Configuration.userAgent = userAgent;
    }


}
